package com.aliang.docker.controller;

import com.aliang.docker.entity.ClassiFication;
import com.aliang.docker.service.ClassiFicationService;
import com.aliang.docker.utils.R;
import com.aliang.docker.utils.UserConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@Api(tags = "商品分类管理")
@RestController
@RequestMapping("/classiFication")
public class ClassiFicationController {
    @Autowired
    ClassiFicationService classiFicationService;

    /**
     * 商品分类
     */
    @ApiOperation("分页查询所有数据")
    @GetMapping("/list")
    public R List() {
        List<ClassiFication> userList = classiFicationService.findName();
        userList.forEach(System.out::println);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", userList);
        return R.ok(map);
    }

    /**
     * 新增商品分类
     */
    @ApiOperation("分类添加")
    @GetMapping("/add")
    public R add(@Validated ClassiFication addFica) {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(classiFicationService.checkClassiNameUnique(addFica))) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("code", 400);
            map.put("data", addFica.getName() + "失败，分类名称已存在");
            return R.ok(map);
        }
        return R.toAdd(classiFicationService.insertPost(addFica));
    }
}
